#include "StdAfx.h"
#include "Math.h"

// static const IID IID_SimpleArithmetic = 
// { 0x32bb8320, 0xb41b, 0x11cf,{0xa6,0xbb,0x0,0x80,0xc7,0xb2,0xd6,0x82} };
// 
// static const IID IID_Trigonometric = 
// { 0x32bb8321, 0xb41b, 0x11cf,{0xa6,0xbb,0x0,0x80,0xc7,0xb2,0xd6,0x82} };
// 
// // {6D4FD2E5-B4BD-4E21-A8E6-50C809B45C97}
// static const CLSID CLSID_Math = 
// { 0x6d4fd2e5, 0xb4bd, 0x4e21, { 0xa8, 0xe6, 0x50, 0xc8, 0x9, 0xb4, 0x5c, 0x97 } };


// IUnknown *CreateInstance()
// {
// 	IUnknown *pIUnknown = static_cast<ISimpleArithmetic*>(new CMath());
// 	if (pIUnknown)
// 	{
// 		pIUnknown->AddRef();
// 		return pIUnknown;
// 	}
// 	return NULL;
// }


CMath::CMath()
{
	m_uRef = 0;
}

CMath::~CMath()
{

}

HRESULT __stdcall CMath::QueryInterface( const IID& riid, void **ppvObject )
{
	TraceMsg(L"CMath::QueryInterface\n");
	if (IsEqualIID(riid, IID_IUnknown))
	{
		TraceMsg(L"\tQueryInterface:IID_IUnknown\n");
		*ppvObject = static_cast<ISimpleArithmetic*>(this);
	}
	else if (IsEqualIID(riid, IID_SimpleArithmetic))
	{
		TraceMsg(L"\tQueryInterface:IID_SimpleArithmetic\n");
		*ppvObject = static_cast<ISimpleArithmetic*>(this);
	}
	else if (IsEqualIID(riid, IID_Trigonometric))
	{
		TraceMsg(L"\tQueryInterface:IID_Trigonometric\n");
		*ppvObject = static_cast<ITrigonometric*>(this);
	}
	else
	{
		TraceMsg(L"\tQueryInterface:Unsupported Interface\n");
		*ppvObject = NULL;
		return E_NOINTERFACE;
	}
	static_cast<IUnknown*>(*ppvObject)->AddRef();
	return S_OK;
}

ULONG __stdcall CMath::AddRef()
{
	TraceMsg(L"CMath::AddRef cRef:%u\n", m_uRef+1);
	return InterlockedIncrement(&m_uRef);
}

ULONG __stdcall CMath::Release()
{
	TraceMsg(L"CMath::Release cRef:%u\n", m_uRef-1);
	if (InterlockedDecrement(&m_uRef) == 0)
	{
		delete this;
		return 0;
	}
	return m_uRef;
}
